/*
obj_var.dm
Variables needed by objects.
*/

mob/player
	proc/drop(obj/gettable/stackable/s)
		if(s.amount>1)
			var/x = input(src, "How many would you like to drop? Max: [s.amount]") as num
			x = min(abs(round(x)),s.amount)
			if(x==s.amount)
				s.Move(loc)
			else if(x>=1)
				new s.type(loc, x)
				s.remove(x)
		else
			s.Move(loc)

		update_contents()

	Entered(obj/gettable/stackable)
		//making sure if it gets deleted by stacking that it doesn't duplicate
		spawn(20)
			if(stackable)
				Query("INSERT INTO `[sql_contentstable]` (amount,owner,type,uid) VALUES ([stackable.amount],'[name]','[stackable.type]','[stackable.uid]');")
		update_contents()

	Exited(obj/gettable/stackable)
		Query("DELETE FROM `[sql_contentstable]` WHERE uid='[stackable.uid]';")
		if(istype(stackable,/obj/gettable/equipment))
			var/obj/gettable/equipment/e = stackable
			e.unequip(src,1)
		update_contents()

obj/gettable/stackable
	DblClick()
		usr << "You have [amount] [src]."
	update()
		..()
		if(ismob(loc))
			Query("UPDATE `[sql_contentstable]` SET amount=[amount] WHERE uid='[uid]';")

obj
	gettable
		var
			droprate
			val
			amount = 1
			uid
			stacksize=1
			cost = 0

		proc
			get(mob/player/c)
				if(Move(c))
					c << "You obtained [src][stacksize>1 ? " (x[src.amount])" : ""]!"

			drop(mob/player/c)
				c.drop(src)

		MouseDrag(loc, cont, params)
			usr.client.mouse_pointer_icon = new /icon(icon, icon_state)

		MouseDrop(src_object,over_object,src_location,over_location,src_control,over_control,params)
			usr.client.mouse_pointer_icon=null

			if(istype(src_object, /obj/shop_related/sell_box))
				var/obj/shop_related/sell_box/s = src_object
				s.sell(usr, src)

			else if(istype(src_object, /obj/shop_related/player_shop/counter))
				var/obj/shop_related/player_shop/counter/mc = src_object
				mc.handle_object(usr, src)

			else if(src_control == "main.map1")
				drop(usr)

		New()
			..()
			uid=gen_uid(name)

obj/dummy/equipped
	icon='base.dmi'
	icon_state="equipped"

obj/gettable/equipment
	var
		slot =""
		str  =0
		vit  =0
		int  =0
		wis  =0
		skill=0
		agi  =0
		luck =0
		equipped
		enchant=""
		enchant_capacity=1

	proc
		add_enchant(gem)
			if(!enchant) return 0
			var/list/ench = params2list(enchant)
			if(ench.len >= enchant_capacity) return 0
			if(gem in ench) return 0
			ench += gem
			enchant = list2params(ench)
			Query("UPDATE `[sql_contentstable]` SET enchant='[enchant]' WHERE uid='[uid]';")
			return 1

		remove_enchant(gem)
			if(!enchant) return 0
			var/list/ench = params2list(enchant)
			if(!(gem in ench)) return 0
			ench -= gem
			enchant = list2params(ench)
			Query("UPDATE `[sql_contentstable]` SET enchant='[enchant]' WHERE uid='[uid]';")
			return 1

		clean_name(gem)
			if(findtext(gem," "))
				return copytext(gem, findtext(gem, " ")+1)
			return gem

		calc_enchant(stat)
			if(!enchant) return 0
			.=0
			for(var/o in params2list(enchant))
				if(enchants[clean_name(o)] == stat)
					. += check_enchant(o)


		get_slots()
			return params2list(slot)

		equip(mob/caller, forced)
			overlays += new/obj/dummy/equipped
			equipped=1
			for(var/o in get_slots())
				caller.equipment[o] = src
			if(!forced) caller << "You equipped [src]."
			if(caller.client)
				var/mob/player/p = caller
				p.update_eqscreen()
				Query("UPDATE `[sql_contentstable]` SET slot='[slot]' WHERE uid='[uid]';")


		unequip(mob/caller, forced)
			if(!equipped) return
			overlays = null
			for(var/o in get_slots())
				caller.equipment[o] = null
			equipped=0
			if(!forced) caller << "You unequipped [src]."
			if(caller.client)
				var/mob/player/p = caller
				p.update_eqscreen()
				Query("UPDATE `[sql_contentstable]` SET slot='' WHERE uid='[uid]';")

	LeftClick(mob/player/caller)
		if(equipped)
			unequip(caller)
			return
		for(var/o in get_slots())
			if(caller.equipment[o])
				var/obj/gettable/equipment/e = caller.equipment[o]
				e.unequip(caller,1)
		equip(caller)